import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        List<String> contentId=new ArrayList<String>();
        String topicId = null;
        String cookies =null;
        String sign=null;
        String Ts=null;
        String topicType=null;
        System.out.println("\t\t\t股票/基金讨论区跟踪软件");
        File configFile=new File("c:\\bbsconfig.txt");
        try {
            InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(configFile),"utf-8");
            BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
            String s = bufferedReader.readLine();
            if(s==null||s.equals(""))return ;
            cookies=bufferedReader.readLine();
            String[] split = s.split("\\|");

            for(int i=1;i<=split.length;i++)
            {
                System.out.println(i+"、"+split[i-1]);
            }
            System.out.print("请选择你要监视的社区：");
            Scanner scanner=new Scanner(System.in);
            int i = scanner.nextInt();
            topicId=split[i-1].split(":")[0];
            Ts=split[i-1].split(":")[1];;
            sign=split[i-1].split(":")[2];
            System.out.println(topicId);
            System.out.println("1.股票");
            System.out.println("2.基金");
            System.out.print("请输入你要选择的类型：");
            int topicTypeIn=scanner.nextInt();
            if(topicTypeIn==1)
            {
                topicType="STOCK";
            }else {
                topicType="FUND";
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //cookies="zone=RZ52B; spanner=rb6u2jimp8JKF84KcYLCQVqK9q3HfjZB; ALIPAYJSESSIONID=RZ52jwGASzU16eCWxMjBKAS46Uc6UY57mobilegwRZ52; rtk=jhANVf+HxTvfVhn2QB8/7/JJNwo0XLYvk0zNGz2TNhoIXLYgQBg; devKeySet={\"apdidToken\":\"tdr9s4p906oAeDK6WOWNrkLXpvYzDqxuEf2j0H2yHvGoQ\\/jXdgEAAA==\"}; cna=lLB8GOvE5AsCAXchwvi7I3Ti\n";
        //topicId="20150718000230030000000000000201";
        String param="[{\"topicType\":\""+topicType+"\",\"showProduction\":1,\"topicId\":\""+topicId+"\",\"lastTime\":0,\"pageSize\":10}]";
        Gson gson=new Gson();
        //
       while (true)
       {
           CloseableHttpClient httpClient= HttpClients.createDefault();
           HttpPost httpPost=new HttpPost("http://mobilegw.alipay.com/mgw.htm");
           httpPost.addHeader("nbappid","60000123");
           httpPost.addHeader("AppId","IOS_ALISTOCK");
           httpPost.addHeader("clientVersion","6.12.1.200");
           httpPost.addHeader("srcSpm","a153.b2503");
           httpPost.addHeader("Did","X/XAkGoIapUDABhcXLSTJ/Ip");
           httpPost.addHeader("nbversion","3.2.2012281349.1");
           httpPost.addHeader("pagets","a153.b2503__NRO14Pz_");
           httpPost.addHeader("Ts",Ts);
           httpPost.addHeader("Cookie",cookies);
           httpPost.addHeader("package_nick","3.2.1080");
           httpPost.addHeader("retryable2","0");
           httpPost.addHeader("bifrost_dto","19000");
           httpPost.addHeader("lastClickSpm","a153.b13013.c31279.1");

           httpPost.addHeader("Accept","*/*");
           httpPost.addHeader("Content-Type","application/json");
           httpPost.addHeader("Accept-Encoding","gzip, deflate, br");
           httpPost.addHeader("scene","active");
           httpPost.addHeader("Sign",sign);
           httpPost.addHeader("User-Agent","%E8%9A%82%E8%9A%81%E8%B4%A2%E5%AF%8C/6.12.1.200 CFNetwork/1128.0.1 Darwin/19.6.0");
           httpPost.addHeader("Operation-Type","alipay.mfinsnsprod.community.forum.getFeedsByForum");
           httpPost.addHeader("x-nb-appid","AF_60000123_ios");
           httpPost.addHeader("Connection","keep-alive");
           StringEntity stringEntity=new StringEntity(param,"utf-8");
           httpPost.setEntity(stringEntity);

           try {
               CloseableHttpResponse execute = httpClient.execute(httpPost);
               if(execute.getStatusLine().getStatusCode()==200)
               {
                   HttpEntity entity = execute.getEntity();
                   String s = EntityUtils.toString(entity);

                   //System.out.println(param);
                  // System.out.println(s);
                   if(s.equals(""))
                   {
                       System.out.println("session失效了");

                       continue;
                   }
                   JsonObject result = gson.fromJson(s, JsonObject.class);
                   JsonArray feedViewItems = result.get("feedViewItems").getAsJsonArray();
                   for(int i=0;i<feedViewItems.size();i++)
                   {
                       JsonObject content = feedViewItems.get(i).getAsJsonObject();
                       String Id = content.get("feedId").getAsString();
                       if(contentId.contains(Id))
                       {
                           continue;
                       }else{
                           if(contentId.size()>=100)
                           {
                               contentId.clear();
                           }
                           contentId.add(Id);
                       }
                       JsonObject contentObj = content.get("contentObj").getAsJsonObject();
                       String sayContent=contentObj.get("contentText").getAsString();
                       String fromContent=contentObj.get("forumName").getAsString();
                       long publishTime = contentObj.get("publishTime").getAsLong();
                       Date creteDate=new Date();
                       creteDate.setTime(publishTime);
                       SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
                       String createTime = sim.format(creteDate);
                       String nick = content.get("userInfo").getAsJsonObject().get("contentUser").getAsJsonObject().get("nick").getAsString();
                       System.out.println(fromContent+"  " +nick+" " +createTime+" 说："+sayContent);
                   }


               }else{
                   System.out.println("请求失败");
               }


           } catch (IOException e) {
               e.printStackTrace();
           }finally {
               try {
                   httpClient.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
           try {
               Thread.sleep(1000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }


    }
}
